
import { _decorator, Component, Node } from 'cc';
import { CategoryConst } from '../ccc3-framework-core/const/Const';
import dataUtil from '../ccc3-framework-core/framework/modules/dataUtil';
import eventUtil from '../ccc3-framework-core/framework/modules/eventUtil';
import { missionModel } from '../ccc3-framework-core/model/missionModel';
import Common from '../ccc3-framework-core/utils/Common';
import { GameStoreDetail } from '../sheets/vo/GameStoreDetail';
import { MissionCfg } from '../sheets/vo/MissionCfg';
import { eventDefine, missionType } from './CustomDefine';
const { ccclass, property } = _decorator;

/**
 * Predefined variables
 * Name = missionUtil
 * DateTime = Thu May 19 2022 14:42:06 GMT+0800 (中国标准时间)
 * Author = laozhou
 * FileBasename = NewComponent.ts
 * FileBasenameNoExtension = missionUtil
 * URL = db://assets/scripts/missionUtil.ts
 * ManualUrl = https://docs.cocos.com/creator/3.4/manual/zh/
 *
 */

@ccclass('missionUtil')
export class missionUtil {
    /**
     * 任务结束
     * @param type 任务类型
     * @param targetId 目标ID
     */
    public static missionEnd(type: missionType, targetId?: number) {
        let currentMission = dataUtil.getCurrentMission();
        let cfg: MissionCfg = MissionCfg.get(currentMission.missionId);
        if (cfg != null) {
            if (cfg.missionType == missionType.GO_MOUNTAIN || cfg.missionType == missionType.SOLD) {
                if (cfg.missionType == type) {
                    currentMission.missionTarget++;
                    if (currentMission.missionTarget >= cfg.missionTarget) {
                        currentMission.missionTarget = cfg.missionTarget;
                        currentMission.state = 1;
                    }
                    dataUtil.setCurrentMission(currentMission, true, false);
                }
            }
            else if (cfg.missionType == missionType.GAME_LEVEL) {
                if (dataUtil.getCurrentLevel() + 1 >= cfg.missionTarget) {
                    currentMission.state = 1;
                    dataUtil.setCurrentMission(currentMission, true, false);
                }
            }
            else if (cfg.missionType == missionType.STALLS_LEVEL) {
                let maxLevel = dataUtil.getRacksMaxLevel() + 1;
                if (maxLevel >= cfg.missionTarget) {
                    currentMission.state = 1;
                    dataUtil.setCurrentMission(currentMission, true, false);
                }
            }
            else if (cfg.missionType == missionType.RECEIVE_TARGET) {
                if (cfg.missionType == type) {
                    if (cfg.missionTarget == targetId) {
                        currentMission.missionTarget++;
                        if (currentMission.missionTarget >= cfg.missionTarget) {
                            currentMission.missionTarget = cfg.missionTarget;
                            currentMission.state = 1;
                        }
                        dataUtil.setCurrentMission(currentMission, true, false);
                    }
                }

            }
            else if (cfg.missionType == missionType.RECEIVE_FOODS) {
                if (type == missionType.RECEIVE_FOODS || type == missionType.RECEIVE_TARGET) {
                    currentMission.missionTarget++;
                    if (currentMission.missionTarget >= cfg.missionTarget) {
                        currentMission.missionTarget = cfg.missionTarget;
                        currentMission.state = 1;
                    }
                    dataUtil.setCurrentMission(currentMission, true, false);
                }
            }

            else if (cfg.missionType == missionType.MAP_TARGET) {
                if (cfg.missionType == type) {
                    currentMission.missionTarget++;
                    if (dataUtil.isUnlockByDetailId(cfg.missionTarget)) {
                        currentMission.missionTarget = cfg.missionTarget;
                        currentMission.state = 1;
                    }
                    dataUtil.setCurrentMission(currentMission, true, false);
                }

            }
            eventUtil.dispatchEvent(eventDefine.UPDATE_MISSION, null);
        }
    }

    public static formatMission(cfg: MissionCfg, mission: missionModel): { progress: number, progressStr: string, memoStr: string } {
        let progress: number = 0;
        let memoStr: string = "";
        let v = `${mission.missionTarget}/${cfg.missionTarget}`;
        switch (cfg.missionType) {
            case missionType.GO_MOUNTAIN:
                progress = mission.missionTarget / cfg.missionTarget;
                memoStr = Common.format(cfg.memo, cfg.missionTarget);
                break;
            case missionType.GAME_LEVEL:
                progress = (dataUtil.getCurrentLevel() + 1) / cfg.missionTarget;
                if (progress > 1)
                    progress = 1;
                v = `${(dataUtil.getCurrentLevel() + 1)}/${cfg.missionTarget}`;
                memoStr = Common.format(cfg.memo, cfg.missionTarget);
                break;
            case missionType.SOLD:
                progress = mission.missionTarget / cfg.missionTarget;
                memoStr = Common.format(cfg.memo, cfg.missionTarget);
                break;
            case missionType.STALLS_LEVEL:
                v = "";
                progress = (dataUtil.getRacksMaxLevel() + 1) / cfg.missionTarget;
                if (progress > 1)
                    progress = 1;
                memoStr = Common.format(cfg.memo, cfg.missionTarget);
                break;
            case missionType.RECEIVE_FOODS:
                progress = mission.missionTarget / cfg.missionTarget;
                memoStr = Common.format(cfg.memo, cfg.missionTarget);
            case missionType.RECEIVE_TARGET:
                if (!Common.isEmpty(cfg.missionTarget)) {
                    let goods = GameStoreDetail.getStoreDetailByDetailId(cfg.missionTarget2);
                    if (goods) {
                        progress = dataUtil.getLocalDetailNumById(cfg.missionTarget2) / cfg.missionTarget;
                        memoStr = Common.format(cfg.memo, cfg.missionTarget, goods.ProductName);
                    }
                }
                break;

            case missionType.MAP_TARGET:
                let mapInfo = GameStoreDetail.getStoreDetailByDetailId(cfg.missionTarget);
                memoStr = Common.format(cfg.memo, `[${mapInfo.ProductName}]`);
                if (dataUtil.isUnlockByDetailId(cfg.missionTarget)) {
                    v = "1/1";
                    progress = 1;
                }
                else {
                    v = "0/1"
                    progress = 0;
                }
            default:
                break;
        }
        return {
            progress,
            progressStr: v,
            memoStr
        };
    }
}